﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Home : System.Web.UI.MasterPage
{
    public string basePath;
    protected void Page_Load(object sender, EventArgs e)
    {
        basePath = Application["APP_PATH"].ToString();
        if (!IsPostBack)
        {
            initSearch(); 
        }
        

    }

    private void initSearch()
    {
        sltCompany.DataSourceID = "sqlGetAllCompanies";
        sltCompany.DataTextField = "Name";
        sltCompany.DataValueField = "Id";
        ListItem defaultItem = new ListItem();
        defaultItem.Text = "--Chọn--";
        defaultItem.Value = "0";
        defaultItem.Selected = true;
        
        sltCompany.DataBind();
        sltCompany.Items.Add(defaultItem);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        int MethodSearch;
        int CompanyId = 0;
        String KeyWord;
        float PriceFrom;
        float PriceTo = 0;
        int TypeSearch = 0;

        if (!IsPostBack)
        { 
            MethodSearch = 0;
            CompanyId = 0;
            KeyWord = "";
            PriceFrom = 0;
            PriceTo = 0;
            TypeSearch = 0;
        }
        

        KeyWord = txtSearch.Text;

        if (sltMethod.SelectedIndex == 0)
        {
            MethodSearch = 0;
        }
        else
        {
            MethodSearch = 1;
            
        }
        if (sltFrom.SelectedIndex == 0)
        {
            PriceFrom = 0;
            if (sltTo.SelectedIndex == 0 || sltTo.SelectedIndex == 4)
            {
                TypeSearch = 0;
            }
            else
            {
                PriceTo = float.Parse(sltTo.Items[sltTo.SelectedIndex].Value.ToString());
            }
        }
        else {
            PriceFrom = float.Parse(sltFrom.Items[sltFrom.SelectedIndex].Value.ToString());
            if (sltTo.SelectedIndex == 0 || sltTo.SelectedIndex == 4)
            {
                TypeSearch = 1;
            }
            else
            {
                PriceTo = float.Parse(sltTo.Items[sltTo.SelectedIndex].Value.ToString());
                TypeSearch = 2;
            }
        }
        CompanyId = int.Parse(sltCompany.Items[sltCompany.SelectedIndex].Value.ToString());
        String urlSearch = "~/SearchResult.aspx?" + "MethodSearch=" + MethodSearch.ToString()+"&CompanyId="+CompanyId + "&KeyWord=" + KeyWord + "&PriceFrom=" + PriceFrom.ToString() + "&PriceTo=" + PriceTo.ToString() +"&TypeSearch=" + TypeSearch.ToString();
        Response.Redirect(urlSearch);
    }
}
